// src/api/ReservationApi.ts
import { BaseApiClient } from '../BaseApi.ts';
import type { ReservationData, ApiResponse } from '../../types/DataTypes.ts';

// 预约相关API
export class ReservationApi extends BaseApiClient {
    constructor() {
        super();
    }

    // 获取所有预约
    async getAllReservations(): Promise<ApiResponse<ReservationData[]>> {
        return this.request<ReservationData[]>({
            method: 'GET',
            url: '/reservations',
        });
    }

    // 搜索预约（按客户名和预约类型）
    async searchReservations(
        customerName?: string,
        reservationTypes?: number[]): Promise<ApiResponse<ReservationData[]>> {
        const params = new URLSearchParams();
        
        if (customerName) {
            params.append('customerName', customerName);
        }
        
        if (reservationTypes && reservationTypes.length > 0) {
            params.append('reservationTypes', reservationTypes.join(','));
        }

        return this.request<ReservationData[]>({
            method: 'GET',
            url: `/reservations/search?${params.toString()}`,
        });
    }

    // 根据客户名获取预约
    async getReservationsByCustomer(customerName: string): Promise<ApiResponse<ReservationData[]>> {
        return this.request<ReservationData[]>({
            method: 'GET',
            url: `/reservations/customer/${encodeURIComponent(customerName)}`,
        });
    }

    // 新增预约
    async addReservation(
        customerName: string,
        reservationType: number,
        reservationMessage: string
    ): Promise<ApiResponse> {
        return this.request({
            method: 'POST',
            url: '/reservations',
            data: {
                customerName,
                reservationType,
                reservationMessage
            },
        });
    }

    // 取消预约
    async cancelReservation(reservationKey: string): Promise<ApiResponse> {
        return this.request({
            method: 'DELETE',
            url: '/reservations/cancel',
            data: { reservationKey },
        });
    }

    // 检查预约是否存在
    async checkReservationExists(reservationKey: string): Promise<ApiResponse<{ exists: boolean }>> {
        return this.request<{ exists: boolean }>({
            method: 'GET',
            url: `/reservations/exists/${encodeURIComponent(reservationKey)}`,
        });
    }
}

// 创建单例实例
export const reservationApi = new ReservationApi();

// 默认导出
export default reservationApi;